#define _CRT_SECURE_NO_WARNINGS 1
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode SL;
struct ListNode* detectCycle(struct ListNode* head) {
    SL* slow, * fast, * pcur;
    slow = fast = head;
    pcur = head;
    int flag = 0;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
        if (fast == slow) {
            flag = 1;
            break;
        }
    }
    if (flag == 0) {
        return NULL;
    }
    else {
        while (pcur != slow) {
            pcur = pcur->next;
            slow = slow->next;
        }
        return pcur;
    }
}